package com.xiaobai.netease_news.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.qq.QQ;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.xiaobai.netease_news.R;
import com.xiaobai.netease_news.activities.LoginActivity;
import com.xiaobai.netease_news.utils.RequestManager;

/**
 * A simple {@link Fragment} subclass.
 */
public class ProfileFragment extends Fragment implements View.OnClickListener {

    private TextView lijidenglu;

    private RequestQueue requestQueue;
    private ImageLoader imageLoader;
    private ImageView yonghutouxiang;
    private Platform sina;
    private Platform tengxunweibo;
    private Platform qq;


    public ProfileFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        RequestManager instance = RequestManager.getInstance(getActivity());
        requestQueue = instance.getRequestQueue();
        imageLoader = instance.getImageLoader();
        ShareSDK.initSDK(getActivity());


        sina = ShareSDK.getPlatform(SinaWeibo.NAME);
        tengxunweibo = ShareSDK.getPlatform("TencentWeibo");
        qq = ShareSDK.getPlatform(QQ.NAME);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_profile, container, false);
        lijidenglu = (TextView) view.findViewById(R.id.lijidenglu);
        yonghutouxiang = (ImageView) view.findViewById(R.id.yonghutouxiang);
        Button tuichudenglu = (Button) view.findViewById(R.id.tuichudenglu);
        yonghutouxiang.setOnClickListener(this);
        lijidenglu.setOnClickListener(this);
        tuichudenglu.setOnClickListener(this);
        return view;
    }


    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.yonghutouxiang:
            case R.id.lijidenglu:
                Intent intent = new Intent(getActivity(), LoginActivity.class);
                startActivityForResult(intent, 333);
                break;
            case R.id.tuichudenglu:

                if (sina.isValid()) {
                    sina.removeAccount();
                    Toast.makeText(getActivity(), "清除新浪微博", Toast.LENGTH_SHORT).show();
                    yonghutouxiang.setImageResource(R.mipmap.live_live_loading_avatar);
                    lijidenglu.setText("立即登录");
                }
                if (qq.isValid()) {
                    qq.removeAccount();
                    Toast.makeText(getActivity(), "清除QQ", Toast.LENGTH_SHORT).show();
                    yonghutouxiang.setImageResource(R.mipmap.live_live_loading_avatar);
                    lijidenglu.setText("立即登录");
                }
                if (tengxunweibo.isValid()) {
                    tengxunweibo.removeAccount();
                    Toast.makeText(getActivity(), "清除腾讯微博", Toast.LENGTH_SHORT).show();
                    yonghutouxiang.setImageResource(R.mipmap.live_live_loading_avatar);
                    lijidenglu.setText("立即登录");
                }
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (requestCode == 333 && resultCode == 444) {
            Bundle bundle = data.getExtras();
            String platform = bundle.getString("platform");
            String touxiang;
            String yonghuming;
            if (platform.equals("QQ")) {

                touxiang = bundle.getString("figureurl_qq_2");
                yonghuming = bundle.getString("nickname");
                String sheng = bundle.getString("province");
                String city = bundle.getString("city");
                lijidenglu.setText(yonghuming + "," + sheng + "," + city);
                imageLoader.get(touxiang, ImageLoader.getImageListener(yonghutouxiang,
                        R.mipmap.live_live_loading_avatar, R.mipmap.live_live_loading_avatar));

            } else if (platform.equals("SinaWeibo")) {
                yonghuming = bundle.getString("name");
                touxiang = bundle.getString("avatar_large");
                Log.d("------>", "volley要用的网址" + touxiang);
                lijidenglu.setText(yonghuming);
                imageLoader.get(touxiang, ImageLoader.getImageListener(yonghutouxiang,
                        R.mipmap.live_live_loading_avatar, R.mipmap.live_live_loading_avatar));

            }
        }
    }
}
